import { resolve } from 'path';
import vue from '@vitejs/plugin-vue';
import customVariables from '/@/theme/custom-variables.js';
import { defineConfig, loadEnv } from 'vite';
import { Buffer } from 'buffer';
import EventEmitter from 'events';

const pathResolve = (dir) => {
  return resolve(__dirname, '.', dir);
};
export default defineConfig(({ mode }) => {

  const env = loadEnv(mode, process.cwd())
  return {
    base: process.env.NODE_ENV === 'production' ? '/' : '/',
    root: process.cwd(),
    resolve: {
      alias: [
        // 国际化替换
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        // 绝对路径重命名：/@/xxxx => src/xxxx
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
        {
          find: /^~/,
          replacement: '',
        },
        // 添加对 events 和 buffer 的别名配置
        {
          find: 'events',
          replacement: resolve(__dirname, 'node_modules/events'),
        },
        {
          find: 'buffer',
          replacement: resolve(__dirname, 'node_modules/buffer'),
        },
      ],
    },
    server: {
      host: '0.0.0.0',
      port: 8080,
      proxy: {
        '/api': {
          target: env.VITE_APP_API_URL,
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
        '/easyLiveApi': {
          target: "http://101.91.156.132:10086",
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/easyLiveApi/, '/api/'),
        },
        '/wsApi': {
          target: "ws://101.66.174.148:9008",
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/wsApi/, '/'),
        },
        '/hlsApi': {
          target: "http://101.66.174.148:9008",
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/hlsApi/, '/'),
        },
        '/wvpApi': {
          target: "http://101.66.174.148:9088",
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/wvpApi/, '/api/'),
        },
        '/wvpFileApi': {
          target: "https://101.66.174.148:9089",
          changeOrigin: true, // 是否改变源地址
          rewrite: (path) => path.replace(/^\/wvpFileApi/, '/'),
        },
      },
    },
    plugins: [vue()],
    optimizeDeps: {
      include: ['ant-design-vue/es/locale/zh_CN', 'dayjs/locale/zh-cn', 'ant-design-vue/es/locale/en_US', 'mqtt'],
      exclude: ['vue-demi'],
    },
    build: {
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        output: {
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: '[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          },
        },
      },
      target: 'modules',
      outDir: 'dist',
      assetsDir: 'assets',
      assetsInlineLimit: '4096',
      chunkSizeWarningLimit: 500,
      minify: 'terser',
      emptyOutDir: true,
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: customVariables,
          javascriptEnabled: true,
        },
      },
    },
    define: {
      __INTLIFY_PROD_DEVTOOLS__: false,
      'process.env': process.env,
      // 'process.env': {},
      // 'window.Buffer': Buffer,
      // 'window.EventEmitter': EventEmitter,
    },
  }
});
